﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using DryIoc;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SoonCode.Core;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Views;
using MessageBox = HandyControl.Controls.MessageBox;

namespace SoonCode.FocusTest.ViewModels
{
    class MemberFeeListViewModel:BindableBase,IDialogAware
    {
        private string memberId;
        private ObservableCollection<FeeInfo> feeList=new ObservableCollection<FeeInfo>();
        private IDialogService dialogService;
        public ObservableCollection<FeeInfo> FeeList
        {
            get { return feeList; }
            set
            {
                feeList = value;
                RaisePropertyChanged();
            }
        }
        public ICommand CloseDialogCommand { get; set; }
        public ICommand AddDataCommand { get; set; }
        public ICommand DeleteMemberCommand { get; set; }
        public ICommand ModifyMemberCommand { get; set; }
        public ICommand ReloadDataCommand { get; set; }
        public MemberFeeListViewModel(IDialogService dialogService)
        {
            CloseDialogCommand = new DelegateCommand(() => RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)));
            AddDataCommand = new DelegateCommand<string>(EditData);
            DeleteMemberCommand = new DelegateCommand<string>(DeleteData);
            ModifyMemberCommand = new DelegateCommand<string>(EditData);
            ReloadDataCommand=new DelegateCommand(()=>{GetData();});
            this.dialogService = dialogService;
        }

        private async void DeleteData(string p)
        {
            if (MessageBox.Show("确定要删除数据吗？", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Question) ==
                MessageBoxResult.Yes)
            {
                var res = await API.DeleteFee(p);
                if (res)
                {
                    Task.Delay(500);
                    await Task.Run(GetData);
                    
                }
            }
        }
        private void EditData(string p)
        {
            DialogParameters param = new DialogParameters(); 
            FeeInfo fee = new FeeInfo();
            fee.memberId=memberId;
            if (!p.IsNullOrEmpty())
            {
                fee = FeeList.FirstOrDefault(f => f.id == p);
                param.Add("fee",fee);
            }
            param.Add("fee", fee);
            dialogService.ShowDialog(nameof(MemberFeeEditView), param, async (res) =>
            {
                if (res.Result == ButtonResult.OK)
                {
                    await Task.Run(GetData);
                }
                
            }, "myDialogWindow");
        }
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        private async void GetData()
        {
            
            var list = await API.GetFeeList(this.memberId);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].index = i;
            }

            this.FeeList = list;
        }
        public void OnDialogOpened(IDialogParameters parameters)
        {
            this.memberId = parameters.GetValue<string>("memberId");
            GetData();
        }

        public string Title { get; } = "费用管理";
        public event Action<IDialogResult>? RequestClose;
    }
}
